﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using JumbotPay.API.Alipay.Mobile;
using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
namespace JumbotPay.WebFile.Alipay_Mobile
{
    public partial class _default : JumbotPay.UI.BasicPage
    {
        public string GenerateOutTradeNo()
        {
            var ran = new Random();
            return string.Format("{0}{1}", DateTime.Now.ToString("yyyyMMddHHmmss"), ran.Next(999));
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            ////////////////////////////////////////////请求参数////////////////////////////////////////////

            //必填参数//
            string appid = q("appid");//appid
            string order_id = q("order_id");//订单号
            string sign = q("sign");
            string key = new JumbotPay.DAL.AppDAL().GetKey(appid);
            if (key == "")
            {
                Response.Write("无效的appid");
                Response.End();
            }
            string[] array0 ={
			    "order_id="+order_id,
                 "appid="+appid
				};
            if (!JumbotPay.Utils.Strings.CheckSign(array0, key, sign))
            {
                Response.Write("验签失败");
                Response.End();
            }
            object[] _value = new JumbotPay.DAL.OrderDAL().GetOrderFields("alipay", order_id, "", "product_name,product_desc,pay_amount,account_alipay");
            string subject = _value[0].ToString();                            //商品名称
            string body = _value[1].ToString();                                 //商品描述
            string total_amout = _value[2].ToString();
            string out_trade_no = order_id;

            string account_alipay = _value[3].ToString();
            //string out_trade_no = GenerateOutTradeNo();
            //new JumbotPay.DAL.OrderDAL().UpdateOutOrderNO(order_id, out_trade_no);

            Config conf = new Config(account_alipay);
            DefaultAopClient client = new DefaultAopClient(
                conf.gatewayUrl, 
                conf.app_id, 
                conf.private_key, 
                "json", 
                "1.0",
                conf.sign_type,
                conf.alipay_public_key,
                conf.charset, 
                false);



            // 支付中途退出返回商户网站地址
            string quit_url = "http://www.knowfar.net.cn/";

            // 组装业务参数model
            AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
            model.Body = body;
            model.Subject = subject;
            model.TotalAmount = total_amout;
            model.OutTradeNo = out_trade_no;
            model.ProductCode = "QUICK_WAP_WAY";
            model.QuitUrl = quit_url;

            AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
            // 设置支付完成同步回调地址
            request.SetReturnUrl(conf.return_url);
            // 设置支付完成异步通知接收地址
            request.SetNotifyUrl(conf.notify_url);
            // 将业务model载入到request
            request.SetBizModel(model);

            AlipayTradeWapPayResponse response = null;
            try
            {
                response = client.pageExecute(request, null, "post");
                Response.Write(response.Body);
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
    }
}
